//
//  HWRenqiOrZuixinOrYuGaoView.m
//  HWanGame
//
//  Created by Chendaobin on 2019/8/26.
//  Copyright © 2019年 Chendaobin. All rights reserved.
//

#import "HWRenqiOrZuixinOrYuGaoView.h"
#import "HWGameListModel.h"
#import "HWOnlinegameTableCell.h"
#import "HWAdvertisingQCTableViewCell.h"
#import "HWIcon3TableviewCell.h"
 


@interface HWRenqiOrZuixinOrYuGaoView()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak) UIScrollView *contentScrollView;
@property (strong,nonatomic) UITableView *oneTabview;
@property (strong,nonatomic) UITableView *twotableView;
@property (strong,nonatomic) UITableView *threadetablview;


@end


@implementation HWRenqiOrZuixinOrYuGaoView



-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self =[super initWithFrame:frame]) {
        
        [self setUpView];
        
    }
    
    return  self;
}

- (void)QC_refreshData {
    
    
  //  self.oneTabview.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.)
    
    
    CGFloat x = self.xuanzhongTag * [UIScreen mainScreen].bounds.size.width;
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
    
    [self.oneTabview reloadData];
    [self.twotableView reloadData];
    [self.threadetablview reloadData];
    
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.contentScrollView.frame =  CGRectMake(0, 0, self.bounds.size.width, self.frame.size.height);
    self.contentScrollView.tag = 100;
    
    
    self.oneTabview.frame =  CGRectMake(0, 0, self.bounds.size.width, self.frame.size.height);
    self.twotableView.frame =  CGRectMake( kScreenW, 0, self.bounds.size.width, self.frame.size.height);
    self.threadetablview.frame = CGRectMake(kScreenW*2, 0, self.bounds.size.width, self.frame.size.height);

    NSLog(@"里面的高度 %lf",self.frame.size.height);
    
    
}



- (void)setUpView {
    
    [self setupContentScrollView];
    
    //添加三个tabview
    [self addTheradtabview];
    

    
}




#pragma mark - 添加内容滚动视图
- (void)setupContentScrollView
{
    // 创建contentScrollView
    UIScrollView *contentScrollView = [[UIScrollView alloc] init];
    CGFloat y = 0;
    contentScrollView.frame = CGRectMake(0, y, self.bounds.size.width, self.frame.size.height);
    [self addSubview:contentScrollView];
    
    _contentScrollView = contentScrollView;
    
    // 设置contentScrollView的属性
    // 分页
    self.contentScrollView.pagingEnabled = YES;
    // 弹簧
    self.contentScrollView.bounces = NO;
    // 指示器
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    
    contentScrollView.backgroundColor = [UIColor redColor];
    
    // 设置代理.目的:监听内容滚动视图 什么时候滚动完成
    self.contentScrollView.delegate = self;
    
    // 设置内容的滚动范围
    self.contentScrollView.contentSize = CGSizeMake(3 * kScreenW, 0);
    
    
    
}


-(void)addTheradtabview {
    
    
    self.oneTabview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,  self.frame.size.height) style:UITableViewStylePlain];
    self.oneTabview.delegate = self;
    self.oneTabview.dataSource = self;
    self.oneTabview.tag = 10;
    self.oneTabview.scrollEnabled = NO;
    self.oneTabview.tableFooterView = [[UIView alloc]init];
    self.oneTabview.backgroundColor = [UIColor whiteColor];
    
          self.oneTabview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentScrollView addSubview:self.oneTabview];
    
    
    
    
     self.twotableView = [[UITableView alloc]initWithFrame:CGRectMake(kScreenW, 0, kScreenW,   self.frame.size.height) style:UITableViewStylePlain];
     self.twotableView.delegate = self;
     self.twotableView.dataSource = self;
     self.twotableView.tag = 20;
     self.twotableView.scrollEnabled = NO;
     self.twotableView.backgroundColor = [UIColor whiteColor];
       self.twotableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
     self.twotableView.tableFooterView = [[UIView alloc]init];
     [self.contentScrollView addSubview: self.twotableView];
    
    
    
    self.threadetablview = [[UITableView alloc]initWithFrame:CGRectMake(kScreenW*2, 0, kScreenW,   self.frame.size.height) style:UITableViewStylePlain];
    self.threadetablview.delegate = self;
    self.threadetablview.dataSource = self;
    self.threadetablview.scrollEnabled = NO;
    self.threadetablview.tag = 30;
    self.threadetablview.tableFooterView = [[UIView alloc]init];
    
        self.threadetablview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.contentScrollView addSubview:self.threadetablview];
    
    
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    if (tableView.tag == 10) {
         return  self.sentimentMuArr.count;
        
    } else if ( tableView.tag == 20) {
        return  self.latestMutaBleArr.count;
        
    } else {
        
         return  self.trailerMutableArr.count;
    }
    
    return  20;
    
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HWGameListModel *model  = nil;
    if (tableView.tag == 10) {
       model = self.sentimentMuArr[indexPath.row];
        
        
        
        
    } else if ( tableView.tag == 20) {
         model = self.latestMutaBleArr[indexPath.row];
        
    } else {
        
        model = self.trailerMutableArr[indexPath.row];
    }
    
    
    if ([model.guanggao isEqualToString:@"0"]) {
        
        static NSString *cellID = @"cellID";
        HWOnlinegameTableCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            cell =  [[NSBundle mainBundle]loadNibNamed:@"HWOnlinegameTableCell" owner:nil options:nil].lastObject;
        }
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        
        cell.backgroundColor = [UIColor whiteColor];
        
        
        if ([self.judgeStr isEqualToString:@"3"]) {
            
           //  [cell.dowunlodeBut setTitle:@"打开" forState:UIControlStateNormal];
            
             NSString *chakan =   [HWjianzhiduiObject keylocalization:@"打开"];
          //   [cell.dowunlodeBut setTitle:@"打开" forState:UIControlStateNormal];
            [cell.dowunlodeBut setTitle:chakan forState:UIControlStateNormal];
            
            
        }else {
             NSString *chakan =   [HWjianzhiduiObject keylocalization:@"查看"];
             
            //  [cell.dowunlodeBut setTitle:@"查看" forState:UIControlStateNormal];
            
            [cell.dowunlodeBut setTitle:chakan forState:UIControlStateNormal];
        }
        
        
        cell.xuanzhongtap = self.xuanzhongTag;
        cell.model = model;
        
        
        return  cell;
        
    } else if ([model.guanggao isEqualToString:@"1"] ) {
        
        static NSString *cellID = @"cellAdID";
        HWAdvertisingQCTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            cell =  [[NSBundle mainBundle]loadNibNamed:@"HWAdvertisingQCTableViewCell" owner:nil options:nil].lastObject;
        }
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
               
        //  cell.model = model;
        cell.model = model;
        
        return  cell;
        
        
    } else {
        //    HWOnlinegameTableCell
        
        static NSString *cellID = @"cellIC3ID";
        HWIcon3TableviewCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            cell =  [[NSBundle mainBundle]loadNibNamed:@"HWIcon3TableviewCell" owner:nil options:nil].lastObject;
        }
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        return  cell;
        
    }
    
    
    
 
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HWGameListModel *model  = nil;
    if (tableView.tag == 10) {
        model = self.sentimentMuArr[indexPath.row];
        
    } else if ( tableView.tag == 20) {
        model = self.latestMutaBleArr[indexPath.row];
        
    } else {
        
        model = self.trailerMutableArr[indexPath.row];
    }
    
    
    
    if ([model.guanggao isEqualToString:@"0"]) {
        
        return  75;
    } else if ([model.guanggao isEqualToString:@"1"]) {
        
        
        return  100 + 10 + 35 ;
    } else {
        
        return  55*CFLScaleValue+10;
        
    }
    
   
    
    
    
    
    
}





- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    if (tableView.tag == 10) {
        
        HWGameListModel *model = self.sentimentMuArr[indexPath.row];
        
        if ([model.guanggao isEqualToString:@"1"]) {
            
            return;
        } else if ([model.guanggao isEqualToString:@"2"]) {
            
             return;
        }
        HWGameDetailController  *xiangqing = [[HWGameDetailController alloc]init];
        xiangqing.gameId = model.id;
        UINavigationController *na =  [self currentNC];
        
        [na pushViewController:xiangqing animated:YES];
       
        DLog(@"人气热门 %ld",indexPath.row);
        
        
        
    } else if (tableView.tag == 20) {
        
        HWGameListModel *model = self.latestMutaBleArr[indexPath.row];
        
        if ([model.guanggao isEqualToString:@"1"]) {
            
            return;
        } else if ([model.guanggao isEqualToString:@"2"]) {
            
            return;
        }
        
        HWGameDetailController  *xiangqing = [[HWGameDetailController alloc]init];
        xiangqing.gameId = model.id;
        UINavigationController *na =  [self currentNC];
               
        [na pushViewController:xiangqing animated:YES];
        
        
        DLog(@"最新上架 %ld",indexPath.row);
        
        
    } else if (tableView.tag ==  30) {
        
        
        HWGameListModel *model = self.trailerMutableArr[indexPath.row];
        
        if ([model.guanggao isEqualToString:@"1"]) {
            
            return;
        } else if ([model.guanggao isEqualToString:@"2"]) {
            
            return;
        }
        
        HWGameDetailController  *xiangqing = [[HWGameDetailController alloc]init];
             xiangqing.gameId = model.id;
       UINavigationController *na =  [self currentNC];
                    
       [na pushViewController:xiangqing animated:YES];
             
        
          DLog(@"新游戏预告 %ld",indexPath.row);
        
    }
    
   
    
    
}













#pragma  mark --------  UIScrollViewDelegate



// 只要一滚动就需要字体渐变
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 字体缩放 1.缩放比例 2.缩放哪两个按钮
    NSInteger leftI = scrollView.contentOffset.x / kScreenW;
    
    
    
//    NSLog(@"54滑动 %ld",leftI);
//    NSString *rolling = [NSString stringWithFormat:@"%ld",leftI];
//    NSDictionary *dict = @{@"rolling":rolling};
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"SentimentOnTheSlide" object:nil userInfo:dict];
//    
    
    
    
 //   UIButton *leftBtn = self.titleButtons[leftI];
    
    // 1.标题颜色 变成 红色
  //  [self selButton:leftBtn];
    
    
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger leftI = scrollView.contentOffset.x / kScreenW;
    NSLog(@"54滑动 %ld",leftI);
    
    if (scrollView.tag == 100) {
        
        NSString *rolling = [NSString stringWithFormat:@"%ld",leftI];
        
        NSDictionary *dict = @{@"rolling":rolling,@"judgeStr":self.judgeStr};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"SentimentOnTheSlide" object:nil userInfo:dict];
        
    }
   
    
    
}


- (UINavigationController *)currentNC
{
    if (![[UIApplication sharedApplication].windows.lastObject isKindOfClass:[UIWindow class]]) {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [self getCurrentNCFrom:rootViewController];
}

//递归
- (UINavigationController *)getCurrentNCFrom:(UIViewController *)vc
{
    if ([vc isKindOfClass:[UITabBarController class]]) {
        UINavigationController *nc = ((UITabBarController *)vc).selectedViewController;
        return [self getCurrentNCFrom:nc];
    }
    else if ([vc isKindOfClass:[UINavigationController class]]) {
        if (((UINavigationController *)vc).presentedViewController) {
            return [self getCurrentNCFrom:((UINavigationController *)vc).presentedViewController];
        }
        return [self getCurrentNCFrom:((UINavigationController *)vc).topViewController];
    }
    else if ([vc isKindOfClass:[UIViewController class]]) {
        if (vc.presentedViewController) {
            return [self getCurrentNCFrom:vc.presentedViewController];
        }
        else {
            return vc.navigationController;
        }
    }
    else {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
}


@end
